﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM

using System.Collections.Generic;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity.OrderFlowManage;
using YunSpace.Domain.IRepository.OrderFlowManage;
using YunSpace.Domain.ViewModel.OrderFlowManage;

#endregion

namespace YunSpace.Repository.OrderFlowManage
{
    public class ClientOrderreserveRepository : RepositoryBase<ClientOrderreserveEntity>, IClientOrderreserveRepository
    {

        public List<ClientOrderReserveDto> GetClientOrderReserveDtos(Pagination pagination, string orderId,string F_CheckUserId,string F_No)
        {
            var sql = $@"
                SELECT r.F_Id, r.F_OrderId,date_format(r.F_StartDate, '%Y-%m-%d') F_StartDate,date_format(r.F_EndDate, '%Y-%m-%d') F_EndDate, r.F_ReserveTime, r.F_State, r.F_Reason, r.F_Remark,
                    s.title AS F_SiteName, s2.title AS F_SpaceName
                    FROM Site_Client_OrderReserve AS r
                    INNER JOIN SL_Site AS s ON s.F_Id = r.F_SiteId
                INNER JOIN SL_Space AS s2 ON s2.F_Id = r.F_SpaceId
                inner join Sys_CheckRecord d on r.F_OrderId=d.F_OrderId and d.F_ApplyType=5 and r.F_No=d.F_No
                left join Site_Client_OrderMapping f on r.F_Id=f.F_DetailId and f.F_ApplyType=d.F_ApplyType and f.F_CheckUserId=d.F_CheckUserId
                WHERE r.F_OrderId = '{orderId}' and d.F_CheckUserId='{F_CheckUserId}' and r.F_No='{F_No}'
                    AND (r.F_DeleteMark = 0 || r.F_DeleteMark IS NULL)
                    AND(s.F_DeleteMark = 0 || s.F_DeleteMark IS NULL)
                    AND(s2.F_DeleteMark = 0 || s2.F_DeleteMark IS NULL)";

            pagination.records = GetClientOrderReserveDtoCount(orderId, F_CheckUserId,F_No);

            var startNo = pagination.rows * (pagination.page - 1);
            var endNo = startNo + pagination.rows;

            sql = $"{sql} ORDER BY r.{pagination.sidx} LIMIT {startNo},{endNo};";

            using (var db = new RepositoryBase())
            {
                var result = db.FindList<ClientOrderReserveDto>(sql);
                return result;
            }
        }

        private int GetClientOrderReserveDtoCount(string orderId,string F_CheckUserId,string F_No)
        {
            var sql = $@"
                SELECT COUNT(1)
                    FROM Site_Client_OrderReserve AS r
                    INNER JOIN SL_Site AS s ON s.F_Id = r.F_SiteId
                INNER JOIN SL_Space AS s2 ON s2.F_Id = r.F_SpaceId
                inner join Sys_CheckRecord d on r.F_OrderId=d.F_OrderId and d.F_ApplyType=5
                left join Site_Client_OrderMapping f on r.F_Id=f.F_DetailId and f.F_ApplyType=d.F_ApplyType and f.F_CheckUserId=d.F_CheckUserId
                WHERE r.F_OrderId = '{orderId}' and d.F_CheckUserId='{F_CheckUserId}' and r.F_No='{F_No}'
                    AND (r.F_DeleteMark = 0 || r.F_DeleteMark IS NULL)
                    AND(s.F_DeleteMark = 0 || s.F_DeleteMark IS NULL)
                    AND(s2.F_DeleteMark = 0 || s2.F_DeleteMark IS NULL);";

            return Count(sql);
        }
    }
}

